package com.szsfm.vehiclemanagement.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.szsfm.vehiclemanagement.R;
import com.szsfm.vehiclemanagement.activity.PhotoViewActivity;
import com.szsfm.vehiclemanagement.util.BitmapUtil;
import com.szsfm.vehiclemanagement.util.DeviceUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoubigbo on 2017/11/22.
 */
public class VehicleImageGroup extends ViewGroup {

    Context mContext;
    final int mImageBounds = DeviceUtil.dip2px(getContext(), 95);
    final int mDeleteButtonBounds = DeviceUtil.dip2px(getContext(), 30);
    public ImageView addButton;
    private int mMargin = DeviceUtil.dip2px(getContext(), 10);
    public int imgSize = 1;
    Uri[] uri = new Uri[3];
    String[] sdPaths = new String[3];

    public VehicleImageGroup(Context context) {
        super(context);
        init();
    }

    public VehicleImageGroup(Context context, AttributeSet attrSet) {
        super(context, attrSet);
        init();
    }

    private void init() {
        mContext = getContext();
        addButton = new ImageView(mContext);
        addButton.setBackground(getResources().getDrawable(R.drawable.ic_add_img));
        addView(addButton);
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int childCount = getChildCount();
        int l = 5;
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view instanceof VehicleImageView || view instanceof ImageView) {
                view.layout(l + mMargin, 0, l + mImageBounds, mImageBounds);
                l += mImageBounds;
            } else if (view instanceof Button) {
                view.layout(l + mMargin / 2 + mImageBounds - mDeleteButtonBounds, 0, l + mImageBounds, mDeleteButtonBounds);
            }
        }
    }

    public void addImage(final Uri bitmap) {
        uri[imgSize-1] = bitmap;
        if (bitmap == null) return;
        final VehicleImageView imageView = new VehicleImageView(mContext);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoViewActivity.startSelf(mContext, bitmap);
            }
        });
        Glide.with(mContext).load(bitmap).into(imageView);

        final Button deleteButton = new Button(mContext);
        deleteButton.setBackground(getResources().getDrawable(R.drawable.ic_del));
        deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeView(deleteButton);
                removeView(imageView);
                invalidate();
                imgSize--;
            }
        });
        addView(deleteButton);
        addView(imageView);
        invalidate();
        imgSize++;
    }

    public void addImage(final String bitmap) {
        sdPaths[imgSize-1]=bitmap;
        if (bitmap == null) return;
        final VehicleImageView imageView = new VehicleImageView(mContext);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoViewActivity.startSelf(mContext, bitmap);
            }
        });
        Glide.with(mContext).load(bitmap).into(imageView);

        final Button deleteButton = new Button(mContext);
        deleteButton.setBackground(getResources().getDrawable(R.drawable.ic_del));
        deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeView(deleteButton);
                removeView(imageView);
                invalidate();
                imgSize--;
            }
        });
        addView(deleteButton);
        addView(imageView);
        invalidate();
        imgSize++;
    }

    public List<byte[]> getImagesBuffer() {
        List<byte[]> lst = new ArrayList<>();
        for (int i = 0; i < uri.length; i++) {
            Uri img_uri = uri[i];
            Bitmap bmp = BitmapUtil.getBitmap(mContext,img_uri);
            if (bmp!=null){
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 60,os);
                byte[] bytes = os.toByteArray();
                lst.add(bytes);
            }
        }
        return lst;
    }

    public List<File> getImageFiles() {
        List<File> lst = new ArrayList<>();
        for (int i=0;i<sdPaths.length;i++){
            File f=new File(sdPaths[i]);
            lst.add(f);
        }
        return lst;
    }

}
